$("#shoppppp").html("")


//在后面插得整合函数操作前端用的
function olo() {
    // $("#checkallid").checkAllBind($("#t :checkbox:gt(0)")); 

    totalMoney();
    //减
    $(".reduce").click(function () {
        changeNum(this, "-")
    });
    //加
    $(".add").click(function () {
        changeNum(this, "+")
    });

    $(".delete").click(function () {
        if (confirm("亲，您真的要删除吗？")) {
            this.parentNode.parentNode.remove();
            totalMoney();
        }
    });

    $("#t :checkbox:gt(0)").change(function () {
        totalMoney();
    });

    $("#checkallid").change(totalMoney);

}





// 修改数字
function changeNum(obj, operator) {
    // 1、获取原来的数字和价格
    let num = obj.parentNode.querySelector(".num").innerHTML;
    let price = obj.parentNode.parentNode.querySelector(".price").innerHTML;

    // 2、修改数量
    operator == "+" ? num++ : num--;
    num = num < 0 ? 0 : num;

    // 3、3、把结果显示在界面上
    // 1）、修改数量
    obj.parentNode.querySelector(".num").innerHTML = num;
    // 2）、修改小计
    obj.parentNode.parentNode.querySelector(".smallTotal").innerHTML = num * price;

    // 4、修改总计
    totalMoney();
}

function totalMoney() {
    let smallTotals = $(".smallTotal");
    let money = 0;
    for (let i = 0; i < smallTotals.length; i++) {
        // 计算总和
        let $checkbox = smallTotals.eq(i).parent().find(":checkbox");

        // console.log($checkbox.prop("checked"));

        if ($checkbox.prop("checked")) {
            money += Number(smallTotals[i].innerHTML);
        }
    }

    $(".totalMoney").html(money);
}

//加号
function add() {
   $.get(
  
       "updateGoodsCount.php",
            
       {
           "vipName":getCookie("username"),
           "goodsId":$(".goodsId").html(),
           "goodsCount":$(".num").html(),
       },
       function (data) {
           console.log(data)
       }, "json");
}



//  接下来用ajax向后端发送数据 
let goodsId = "";
$(function () {

    $.get(
        "getShoppingCart.php",
        {
            "vipName": getCookie("username"),
        },
        function (data) {
            let htmlr = "";
            console.log(data)
            data.forEach(element => {
                //生成模版字符串
                htmlr += `
              <tr>
                <td><input type="checkbox" /></td>
                <td class="goodsId">${element.goodsId}</td>
                <td>
                    <div class="Commodityphoto">
                        <img src=" ${element.goodsImg}" alt="" ddd="">

                    </div>
                </td>
                <td>${element.goodsName}</td>
                <td class="price">${element.goodsPrice}</td>
                <td>
                    <input type="button" value="－" class="reduce" />
                    <span class="num">${element.goodsCount}</span>
                    <input type="button" value="＋" class="add" />
                </td>
                <td class="smallTotal"><span>${element.goodsCount * element.goodsPrice}</span></td>
                <td>
                    <input type="button" value="删除" class="delete" />
                </td>
                </tr>
              `
                $("#t tbody ").html(htmlr);
                //点击删除号可实现前后端的删除
                $(".delete").click(function () {
                    // console.log(1)
                    $.get(
                        "deleteGoods.php",
                        {
                            "vipName": getCookie("username"),
                            "goodsId": $(".goodsId").html(),
                        },
                        function (data) {
                            // console.log(2)
                        }, "json");

                });

                //点击加号后端随之加一个               
                
            })
            olo();
            $(".add").click(add);
            $(".reduce").click(add);


            //点击加减号实现对后端的加减

        }, "json");

})





